package com.ygsoft.tianlu_mall.service.impl;

import com.ygsoft.tianlu_mall.mapper.ShopMapper;
import com.ygsoft.tianlu_mall.pojo.Goods;
import com.ygsoft.tianlu_mall.pojo.Shop;
import com.ygsoft.tianlu_mall.pojo.vo.req.*;
import com.ygsoft.tianlu_mall.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    ShopMapper shopMapper;

    @Override
    public Shop getShopById(int userid){return shopMapper.getShopById(userid);}

    @Override
    @Transactional
    public int checkRealName(int user_id) {
        int isReal =0;
        HashMap<String,Object> map = shopMapper.checkRealName(user_id);
        //信息为空，则未进行实名制
        if (map!=null){
            isReal = 1;
        }else {
            return isReal;
        }
        return isReal;
    }

    @Override
    public void doReal(DoRealReqVO vo) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("user_id",vo.getUser_id());
        map.put("user_realName",vo.getUser_realName());
        map.put("user_idNumber",vo.getUser_idNumber());
        map.put("user_bankCard",vo.getUser_bankCard());
        shopMapper.doReal(map);
    }



    @Override
    public HashMap<String, Object> getRealInfo(int user_id) {
        return shopMapper.checkRealName(user_id);
    }

    @Override
    public void auditReal(int user_id) {
        shopMapper.auditReal(user_id);
    }

    @Override
    public int getUserShop(int user_id) {
        return shopMapper.getUserShop(user_id);
    }

    @Override
    public void openShop(int user_id, String shop_name) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("shop_name",shop_name);
        map.put("shop_createTime",new Date());
        map.put("shop_userid",user_id);
        shopMapper.openShop(map);
    }

    @Override
    public int getShopIdByUserId(int user_id) {
        return shopMapper.getShopIdByUserId(user_id);
    }

    @Override
    @Transactional
    public void addGoods(AddGoodsReqVO vo) {
        Goods goods = new Goods();
        //商品信息
//        BeanUtils.copyProperties(vo,goods);
        goods.setGoods_name(vo.getGoods_name());
        goods.setGoods_price(vo.getGoods_price());
        goods.setGoods_description(vo.getGoods_description());
        goods.setGoods_shopid(vo.getShop_id());
        goods.setGoods_typeid(vo.getType_id());
        addGoodsInfo(goods);
        //获得最新的goodsid
        int newGoodId = getNewGoodId();
        List<AddPropertyValueItemReqVO> listValue = new ArrayList<>();
        listValue = vo.getPropertyValueItemReqVOList();
        //添加商品属性
        setGoodPropertyValue(newGoodId,listValue);


    }


    @Override
    @Transactional
    public void addGoodsInfo(Goods goods) {
        if(goods != null){
            shopMapper.addGoodsInfo(goods);
        }
        else{
            return;
        }
    }

    @Override
    @Transactional
    public int getNewGoodId() {
        return shopMapper.getNewGoodId();
    }

    @Override
    @Transactional
    public String getFolderName(int type_id) {
        return shopMapper.getFolderName(type_id);
    }

    @Override
    @Transactional
    public String getShopName(int shop_id) {
        return shopMapper.getShopName(shop_id);
    }

    @Override
    @Transactional
    public String getGoodsName(int goods_id) {
        return shopMapper.getGoodsName(goods_id);
    }

    @Override
    @Transactional
    public void saveGoodsTitleUrl(int goods_id, String titleUrl) {
        HashMap<String,Object> urlMap = new HashMap<>();
        urlMap.put("goods_id",goods_id);
        urlMap.put("titleUrl",titleUrl);
        shopMapper.saveGoodsTitleUrl(urlMap);
    }

    @Override
    @Transactional
    public void saveGoodsContentUrl(int goods_id, String contentUrl) {
        HashMap<String,Object> urlMap = new HashMap<>();
        urlMap.put("goods_id",goods_id);
        urlMap.put("contentUrl",contentUrl);
        shopMapper.saveGoodsContentUrl(urlMap);
    }

    @Override
    @Transactional
    public void saveGoodsFileUrl(int goods_id, String fileUrl) {
        HashMap<String,Object> urlMap = new HashMap<>();
        urlMap.put("goods_id",goods_id);
        urlMap.put("fileUrl",fileUrl);
        shopMapper.saveGoodsFileUrl(urlMap);
    }


    @Override
    @Transactional
    public void setGoodPropertyValue(int newGoodId, List<AddPropertyValueItemReqVO> listValue) {
        for (AddPropertyValueItemReqVO propertyValueItem : listValue) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("propertyValue_propertyid",propertyValueItem.getProperty_id());
            map.put("propertyValue_goodsid",newGoodId);
            map.put("propertyValue_name",propertyValueItem.getPropertyValue_name());
            shopMapper.setGoodPropertyValue(map);

        }
    }


    @Override
    @Transactional
    //如果存在，则返回true，允许上架
    public Boolean checkGoodsOnDB(int goods_id) {
        List goods = shopMapper.checkGoodsOnDB(goods_id);
        if (goods.size()==0){
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public int checkGoodsStatus(int goods_id) {
        return shopMapper.checkGoodsStatus(goods_id);
    }

    @Override
    @Transactional
    public void goodsUpshelf(int goods_id) {
        //设置上架时间
        HashMap<String,Object> map = new HashMap<>();
        map.put("goods_id",goods_id);
        map.put("goods_uptime",new Date());
        map.put("goods_downtime",null);
        shopMapper.goodsUpshelf(map);
    }

    @Override
    @Transactional
    public void goodsDownshelf(int goods_id) {
        //设置下架时间
        HashMap<String,Object> map = new HashMap<>();
        map.put("goods_id",goods_id);
        map.put("goods_downtime",new Date());
        shopMapper.goodsDownshelf(map);
    }



    @Override
    @Transactional
    public HashMap<String, Object> getUpdateGoodsInfo(int goods_id) {
        List<HashMap<String,Object>>  updateInfoFromGoodsList= shopMapper.getUpdateInfoFromGoods(goods_id);
        List<HashMap<String,Object>>  updateInfoFromPropertyList=shopMapper.getUpdateInfoFromProperty(goods_id);
        String goods2title = shopMapper.getUpdateInfoFromGoods2title(goods_id);
        String goods2content = shopMapper.getUpdateInfoFromGoods2content(goods_id);
        String goods2file = shopMapper.getUpdateInfoFromGoods2file(goods_id);
        HashMap<String,Object> map = new HashMap();
        map.put("updateInfoFromGoodsList",updateInfoFromGoodsList);
        map.put("updateInfoFromPropertyList",updateInfoFromPropertyList);
        map.put("goods2title",goods2title);
        map.put("goods2content",goods2content);
        map.put("goods2file",goods2file);
        return map;
    }

    @Override
    @Transactional
    public void updateGoods(UpdateGoodsReqVO vo) {
        int goods_id = vo.getGoods_id();
        String goods_name = vo.getGoods_name();
        BigDecimal goods_price = vo.getGoods_price();
        String goods_description = vo.getGoods_description();
        String goods_updateLog = vo.getGoods_updateLog();
        HashMap<String,Object> map = new HashMap<>();
        map.put("goods_id",goods_id);
        map.put("goods_name",goods_name);
        map.put("goods_price",goods_price);
        map.put("goods_description",goods_description);
        map.put("goods_updateLog",goods_updateLog);
        updateGoodsInfo(map);
        List<UpdatePropertyValueItemReqVO> listValue = new ArrayList<>();
        listValue = vo.getPropertyValueItemReqVOList();
        updateGoodsPropertyValue(goods_id,listValue);
    }


    @Override
    @Transactional
    public void updateGoodsInfo(HashMap<String, Object> map) {
        shopMapper.updateGoodsInfo(map);
    }

    @Override
    @Transactional
    public void updateGoodsPropertyValue(int goods_id, List<UpdatePropertyValueItemReqVO> listValue) {

        for (UpdatePropertyValueItemReqVO propertyValueItem: listValue){
            HashMap<String, Object> map = new HashMap<>();
            map.put("propertyValue_propertyid",propertyValueItem.getProperty_id());
            map.put("propertyValue_goodsid",goods_id);
            map.put("propertyValue_name",propertyValueItem.getPropertyValue_name());
            shopMapper.updateGoodPropertyValue(map);
        }
    }

    @Override
    public int getTypeId(int goods_id) {
        return shopMapper.getTypeId(goods_id);
    }

    @Override
    public int getShopId(int goods_id) {
        return shopMapper.getShopId(goods_id);
    }

    @Override
    public void updateGoodsTitleUrl(int goods_id, String titleUrl) {
        HashMap<String,Object> urlMap = new HashMap<>();
        urlMap.put("goods_id",goods_id);
        urlMap.put("titleUrl",titleUrl.toString());
        shopMapper.updateGoodsTitleUrl(urlMap);
    }

    @Override
    public void updateGoodsContentUrl(int goods_id, String contentUrl) {
        HashMap<String,Object> urlMap = new HashMap<>();
        urlMap.put("goods_id",goods_id);
        urlMap.put("contentUrl",contentUrl.toString());
        shopMapper.updateGoodsContentUrl(urlMap);
    }

    @Override
    public void updateGoodsFileUrl(int goods_id, String fileUrl) {
        HashMap<String,Object> urlMap = new HashMap<>();
        urlMap.put("goods_id",goods_id);
        urlMap.put("fileUrl",fileUrl.toString());
        shopMapper.updateGoodsFileUrl(urlMap);
    }


    @Override
    @Transactional
    public List<Map<String, Object>> getGoodsByShopId(int shop_id) {
        return shopMapper.getGoodsByShopId(shop_id);
    }

    @Override
    @Transactional
    public List<Map<String, Object>> getGoodsByShopIdAndTypeId(int shop_id, int type_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("shop_id",shop_id);
        map.put("type_id",type_id);
        return shopMapper.getGoodsByShopIdAndTypeId(map);
    }

    @Override
    public List getGoodsIdByShopId(int shop_id) {
        return shopMapper.getGoodsIdByShopId(shop_id);
    }

    @Override
    public List getGoodsIdByShopIdAndTypeId(int shop_id, int type_id) {
        HashMap<String,Object> map = new HashMap<>();
        map.put("shop_id",shop_id);
        map.put("type_id",type_id);
        return shopMapper.getGoodsIdByShopIdAndTypId(map);
    }

    @Override
    public Double getService(int shop_id) {
        Double value = 5.00;
        Double service = shopMapper.getService(shop_id);
        if (service==null) {
            return value;
        }
        return service;
    }

    @Override
    public Double getDescription(int shop_id) {
        Double value = 5.00;
        Double description = shopMapper.getDescription(shop_id);
        if (description==null) {
            return value;
        }
        return description;
    }

    @Override
    public int isCollectShop(int shop_id,int user_id) {
        Integer isCollect = 0;
        List<HashMap<String,Object>> collect2shopList = shopMapper.isCollectShop(user_id);
        for (HashMap collect2shopMap:collect2shopList) {
            if (collect2shopMap.containsValue(shop_id)){
                isCollect = 1;
                return isCollect;
            }
        }
        return isCollect;
    }
}
